public class DoubleWater {
    public static void main(String[] args) {
        int[] height={4,2,0,3,2,5};
        System.out.println(trap(height));
    }
    public static int trap(int[] height) {
        int maxValue=0;
        int result=0;
        int lastIndex=0;
        for(int i=0;i<height.length;i++){
            if(maxValue<height[i]){
                maxValue=height[i];
            }
        }

        for(int i=1;i<=maxValue;i++){
            for(int k=height.length-1;k>=0;k--){
                if(height[k]>=i){
                    lastIndex=k;
                    break;
                }
            }
            boolean flag=false;
            for(int j=0;j<=lastIndex;j++){
                if(height[j]>=i){
                    flag=true;
                }
                if(flag && height[j]<i){
                    result++;
                }
            }
        }
        return result;
    }
}
